package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kang.entity.SysRole;
import com.kang.entity.SysUser;
import com.kang.entity.SysUserRole;
import com.kang.mapper.SysRoleMapper;
import com.kang.mapper.SysUserMapper;
import com.kang.mapper.SysUserRoleMapper;
import com.kang.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.util.AESSimpleUtil;
import com.kang.vo.AssginRoleVo;
import com.kang.vo.SysUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yuhongkang
 * @since 2023-01-04
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public IPage<SysUser> selectSysUserList(Long page, Long limit, SysUserQueryVo sysUserQueryVo) {
        //mybatis-plus传入page不需要在xml中写分页，自动分页
        IPage<SysUser> pageParam = new Page<>(page,limit);
        return sysUserMapper.selectByPage(pageParam,sysUserQueryVo);
    }

    @Override
    public Integer savsSysUser(SysUser sysUser) throws Exception {
        sysUser.setPassword(AESSimpleUtil.encrypt(sysUser.getPassword()));
        sysUser.setType(1);
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public SysUser getUserById(Integer id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public Integer updateSysUser(SysUser sysUser) {
        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public Integer deleteSysUserById(Integer id) {
        return sysUserMapper.deleteById(id);
    }

    @Override
    public Integer updateStatus(Integer id, Integer status) {
        SysUser sysUser = sysUserMapper.selectById(id);
        sysUser.setStatus(status);
        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public Map<String, Object> getRoleId(Integer userId) {
        List<SysRole> sysRoleList = sysRoleMapper.selectList(null);
        //根据用户Id查询已经对应角色
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Integer> roleIdList = sysUserRoleMapper.selectList(wrapper).stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("allRoles",sysRoleList);
        resultMap.put("userRoleIds",roleIdList);
        return resultMap;
    }

    @Override
    public Object doAssign(AssginRoleVo assginRoleVo) {
        //根据用户id删除之前的角色
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",assginRoleVo.getUserId());
        sysUserRoleMapper.delete(wrapper);
        //获取所有的角色，添加到角色用户关系表里
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(assginRoleVo.getUserId());
        for (Integer roleId : assginRoleVo.getRoleIdList()) {
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insert(sysUserRole);
        }

        return null;
    }
}
